package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.TrainStation;
import com.jiawa.train.business.domain.TrainStationExample;
import com.jiawa.train.business.mapper.TrainStationMapper;
import com.jiawa.train.business.req.TrainStationQueryReq;
import com.jiawa.train.business.req.TrainStationSaveReq;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResult;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class TrainStationService {
    //reource是使用名称注入的方式而autowired是使用类型注入的方式
    @Resource
    private TrainStationMapper trainStationMapper;

    //新增或者更新用户信息，通过判断是否有id决定进行新增操作还是更新操作
    public void save(TrainStationSaveReq req) {
        //将传递的参数赋给TrainStation对象,在req中没有id,createTime,updateTime的数据，需要自己进行赋值
        //这里的id使用雪花算法，然后创建时间使用当前时间
        DateTime now = DateTime.now();
        TrainStation trainStation = BeanUtil.copyProperties(req, TrainStation.class);
        //如果id为空，则进行新增操作
        if(ObjectUtil.isNull(trainStation.getId())){
            queryIndexUniqueMethod(trainStation);
            queryNameUniqueMethod(trainStation);
            trainStation.setId(SnowUtil.getSnowflakeNextId());
            trainStation.setCreateTime(now);
            trainStation.setUpdateTime(now);
            trainStationMapper.insert(trainStation);
        }
        //否则进行更新操作
        else{
            trainStation.setUpdateTime(now);
            trainStationMapper.updateByPrimaryKeySelective( trainStation);
        }
    }

    private void queryNameUniqueMethod(TrainStation trainStation) {
        TrainStationExample trainStationExample = new TrainStationExample();
        TrainStationExample.Criteria criteria = trainStationExample.createCriteria();
        criteria.andTrainCodeEqualTo(trainStation.getTrainCode());
        criteria.andNameEqualTo(trainStation.getName());
        if(trainStationMapper.countByExample(trainStationExample) > 0){
            throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_NAME_UNIQUE_ERROR);
        }
    }

    private void queryIndexUniqueMethod(TrainStation trainStation) {
        TrainStationExample trainStationExample = new TrainStationExample();
        TrainStationExample.Criteria criteria = trainStationExample.createCriteria();
        criteria.andTrainCodeEqualTo(trainStation.getTrainCode());
        criteria.andIndexEqualTo(trainStation.getIndex());
        if(trainStationMapper.countByExample(trainStationExample) > 0){
            throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_INDEX_UNIQUE_ERROR);
        }
    }

    //如果用户id存在则进行根据id进行查询乘客的信息，如果是用户id不存在则进行查询所有,全部都按分页进行处理
    public PageResult queryTrainStationList(TrainStationQueryReq  req) {
        //创建trainStationExample对象
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.setOrderByClause("train_code asc,`index` asc");
        //设置查询条件
        TrainStationExample.Criteria criteria = trainStationExample.createCriteria();
        log.info("查询火车车站信息，入参的trainCode：{}",req.getTrainCode());

        //如果传递了火车的车次则按照火车车次进行查询火车车站信息，否则就是查询所有的火车车站的信息
        if(req.getTrainCode() != "" && req.getTrainCode() != null){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }
        // 先设置分页的参数
        PageHelper.startPage(req.getPage(),req.getPageSize());
        //进行查询数据库
        List<TrainStation> trainStationList = trainStationMapper.selectByExample(trainStationExample);
        //返回查询结果,转为分页的形式,
        PageInfo<TrainStation> pageInfo = new PageInfo<>(trainStationList);
        //获取数据进行封装
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        //返回查询结果
        return pageResult;
    }

    public void delete(Long id) {
        trainStationMapper.deleteByPrimaryKey(id);
    }

    public List<TrainStation> selectByTrainCode(String trainCode){
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.createCriteria().andTrainCodeEqualTo(trainCode);
        return trainStationMapper.selectByExample(trainStationExample);
    }
}